<template>
  <el-card>
    <BreadCrumb />
    <el-table
      :data="semesterList"
      :cell-style="{ textAlign: 'center' }"
      :header-cell-style="{ textAlign: 'center' }"
      style="width: 100%"
      stripe
      border
      highlight-current-row
    >
      <el-table-column label="学期名称" min-width="25%">
        <template slot-scope="scope">
          <i class="el-icon-time" />
          <span>{{ scope.row.isCurrentSemester === true ? scope.row.semesterName +'  (当前学期)  ' : scope.row.semesterName }}</span>
        </template>
      </el-table-column>
      <el-table-column label="学期起始日期" min-width="25%">
        <template slot-scope="scope">
          <i class="el-icon-time" />
          <span>{{ scope.row.semesterOriginDate }}</span>
        </template>
      </el-table-column>
      <el-table-column label="学期总周数" min-width="25%">
        <template slot-scope="scope">
          <i class="el-icon-time" />
          <span>{{ scope.row.semesterWeekTotal }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="操作"
        min-width="25%"
      >
        >
        <template slot-scope="scope">
          <el-button size="mini" @click="handleEditSemester(scope.row.id)">
            编辑
          </el-button>
          <el-button
            size="mini"
            type="danger"
            @click="deleteSemester(scope.row.id)"
          >
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
  </el-card>
</template>

<script>
import BreadCrumb from '@/components/BreadCrumb'
import { isConfirm } from '@/utils/confirm'
import { checkAuth } from '@/utils/checkAuth'
import { getSemester, deleteSemester } from '@/api/semesterApi'
export default {
  components: {
    BreadCrumb
  },
  data() {
    return {
      // 学期信息
      semesterList: []
    }
  },
  created() {
    // 获取学期信息
    this.getSemester()
  },
  methods: {
    // 获取学期信息
    async getSemester() {
      const { data: res } = await getSemester()
      this.semesterList = res.items
    },
    // 处理编辑学期信息
    async handleEditSemester(id) {
      if (!await checkAuth('编辑学期')) return
      this.$router.push('/semester/edit/' + id)
    },
    // 删除学期信息
    async deleteSemester(id) {
      if (!await checkAuth('删除学期')) return
      // 确认删除
      if (await isConfirm('此操作将永久删除该学期，是否继续？')) {
        // 发送网络请求进行删除
        await deleteSemester(id)
        this.$message.success('删除成功')
        // 重新加载页面数据
        this.getSemester()
      }
    }
  }
}
</script>

<style scoped>
.el-table{
  font-size: 16px;
}
.el-table__row span {
  margin-left: 10px
}
.el-card{
  min-height:95%;
}
</style>
